 function ghvec=plot_irf(irf_mat,stpos,shockpos,stnames,shonames,addtotit,irfnames,axfont,subplotConfig) 
% =========================================================================
% PLOT_IRF.M 
%  
% ghvec=plot_irf(irf_mat,stpos,shockpos,legends,stnames,shonames,addtotit) 
%
% Given a matrix of IRFS  IRFMAT of dimension [NIRF NPER+1 NX ] 
% NIRF    Number of States 
% NPER    Number of periods 
% NX      Number of pages 
% Note: The dimensions of a 3-dimensional object are referred to as
% row,column and pages. 
% Here the pages correspond to SHOCKS 
% perturbations, plot the IRF to the states indicated in STPOS and to the
% shocks given in SHOCKPOS. Creating one figure for each SHOCK 
% 
% If STPOS (SHOCKPOS) not provided, whole matrix of states (shocks) will be produced 
% 
% Additional inputs 
% ------------------
% LEGENDS       Cell indicating which each line stands for 
% STNAMES       Cell with names of full length state vector (NSTx1) 
% SHONAMES      Cell with names of shocks (NX x 1 ) 
% ADDTOTIT      String to add to the SUPTITLE e.g. STORAGE 
% 
% Output
% GHVEC         Vector with the handles to each figure
%
% Alejandro Justiniano  January 30 2008 
% =========================================================================

% 1. Check dimensions for matrix and assign names to states, shocks and
% legends if not provided 
% =========================================================================
[nst nper nx]=size(irf_mat);nper=nper-1; 
if nargin < 2 || isempty(stpos);stpos=(1:nst)';end 
if nargin < 3 || isempty(shockpos);shockpos=(1:nx)';end
shockpos=sort(shockpos);nxuse=length(shockpos); 
if nxuse > nx; error('SHOCKPOS cannot be greater than NX'); end 
nstuse=length(stpos);
if nstuse > nst; error('STPOS cannot be greater than NST'); end 
if nargin < 9 || isempty(subplotConfig)
    subplotConfig=[ceil(nst/2) 2];
end
nrow=subplotConfig(1); 
ncol=subplotConfig(2); 

% Check Other optional arguments 
% -------------------------------
if nargin < 4 || isempty(stnames)
    stnames=fnumcell('St',(1:nst));
else
    if length(stnames)~=nst;error('STNAMES does not match rows in IRF_MAT');end
end
if nargin < 5 || isempty(shonames);
    shonames=fnumcell('Shock ',(1:nx)); 
else 
    if length(shonames)~=nx;error('SHONAMES does not match columns in IRF_MAT'); end 
end 
if nargin < 6 || isempty(addtotit) 
    addtotit=''; 
else 
    addtotit=char(addtotit);  
end 
if nargin < 7 || isempty(irfnames); 
    flag_irfnames=0; 
else 
    if length(irfnames)~=length(stpos) 
        error('IRFNAMES must match length of STPOS') 
    end 
    flag_irfnames=1; 
end 
if nargin ==8 && ~isempty(axfont);
    set(0,'DefaultAxesFontSize',axfont);
end


% 2. Define General Settings for the PLOTS 
% =========================================================================
style={'-','--',':','-','-*','--.',':*'}; 
width=[ 2    2.5   3  2    2   3     2  ]; 
% Sets the color for each line 
colormatrix=[0      0       1
             0.35   0.35    0.35 
             0.55   0.55    0.55
             0.5    0.5     0.5 
             0.4    0.4     0.4 
             0.3    0.3     0.3 
             0.5    0.5     0.5 ]; 
xvec=(0:nper)';
flag_roomleg=((nstuse/2-nrow)~=0); % Room for Legends in Extra Chart


if nrow < 5
    titfont=12; %Font for titles
elseif nrow < 8
    titfont=10;
elseif nrow < 11
    titfont=8;
else 
    titfont=7; 
end 
set(0,'DefaultAxesFontSize',titfont-1);

% 3. One figure per shock, subfigure per state 
% =========================================================================
ghvec=zeros(nxuse,1);
for ii=1:nxuse;
    figure;
    % One figure for each STATE
    for jj=1:nstuse
        subplot(nrow,ncol,jj);
        % Plot IRFS to each perturbation of the Impulse responses
        temp=squeeze(irf_mat(stpos(jj),:,shockpos(ii)));
        flatten=find(abs(temp) < 1e-12 ); 
        % Make IRFS that are very small == 0 
        if ~isempty(flatten) 
            temp(flatten)=zeros(length(flatten),1); 
        end 
        hand=plot(xvec,temp,style{1},'LineWidth',width(1));
        set(hand,'Color',colormatrix(1,:));
        if flag_irfnames==0
            title(stnames{stpos(jj)},'FontSize',titfont);
        else
            title(irfnames{jj},'FontSize',titfont);
        end
        set(gca,'Box','off')
        %axis tight; 
        xlim([0 nper]); 
        ylimVec=[min(0,min(temp)) max(0,max(temp))];
        yLimLimit=1e-5;
        if ylimVec(2)-ylimVec(1)  < yLimLimit;
            ylimVec=[ylimVec(1)-yLimLimit ylimVec(2)+yLimLimit];
        else
            
            if ylimVec(1) < 0
                ylimVec(1)=1.05*ylimVec(1);
            else
                ylimVec(1)=0.95*ylimVec(1);
            end
            if ylimVec(2) < 0
                ylimVec(2)=0.95*ylimVec(2);
            else
                ylimVec(2)=1.05*ylimVec(2);
            end
        end
            
        tempExtract=assignXTicksQuarterly(xvec,4);
        set(gca,'xTick',xvec(tempExtract));
        ylim(ylimVec); 
        hand=hline(0); 
        set(hand,'color','r','LineWidth',1,'LineStyle','--'); 
        %plotStruct.xtickLabels=histStru.sample.forecastCell(tempExtract);

    end
    % Put legend in new subplot if possible
    % -------------------------------------
    ghvec(ii)=gcf;
    set(ghvec(ii),'PaperPosition',[0.8 0.7 7 9])
    hand=suptitle(['IRF: ',shonames{shockpos(ii)},' ',addtotit]);
    set(hand,'FontSize',titfont+2); %'FontWeight','Bold');
end
set(0,'DefaultAxesFontSize','remove');
